//
//  TopViewOfCell.swift
//  WeiBlog
//
//  Created by xiang on 15/10/20.
//  Copyright © 2015年 xiang. All rights reserved.
//

import UIKit
import SDWebImage
class TopViewOfCell: UIView {
  
    //在赋值的时候,给控件设上内容
    var viewModel:StatusViewModel?{
        didSet{
            
            iconImageView.sd_setImageWithURL(viewModel?.iconURL, placeholderImage: UIImage(named: "avatar_default_big"), options: [SDWebImageOptions.RefreshCached,SDWebImageOptions.RetryFailed])
            
            verifiedImageView.image = viewModel?.verifiedImage
            
            screenNameLabel.text = viewModel?.status.user?.screen_name
            
            vipImageView.image = viewModel?.vipImage
            
            createTimeLabel.text = viewModel?.composetTime
            
//            sourceLabel.text = viewModel?.status.source
            print(viewModel?.status.source)
        }
    }
    init(){
        super.init(frame: CGRectZero)
//        backgroundColor = UIColor.redColor()
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: ----懒加载
    /// 头部分割线
    private lazy var seperateLine:UIView = UIView()
    /// 头像
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 昵称
    private lazy var screenNameLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(13)
        label.textAlignment = NSTextAlignment.Left
        label.textColor = UIColor.darkGrayColor()
        label.sizeToFit()
        return label
        }()
    /// 认证图标
    private lazy var verifiedImageView:UIImageView = UIImageView(image: nil)
    /// vip图标
    private lazy var vipImageView:UIImageView = UIImageView(image: nil)
    /// 创建时间
    private lazy var createTimeLabel:UILabel = UILabel(text: "刚刚", fontSize: 9, color: UIColor.orangeColor())
    /// 来源
    private lazy var sourceLabel:UILabel = UILabel(text: "微博客户端", fontSize: 9)
}


// MARK: - 设置界面
extension TopViewOfCell
{
    private func setUI(){
        
        addSubview(iconImageView)
        addSubview(verifiedImageView)
        addSubview(screenNameLabel)
        addSubview(vipImageView)
        addSubview(createTimeLabel)
        addSubview(sourceLabel)
        addSubview(seperateLine)
        seperateLine.backgroundColor = UIColor(white: 0.8, alpha: 1.0)
        //自动布局
        
        seperateLine.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self)
            make.height.equalTo(margin)
            make.left.equalTo(self)
            make.top.equalTo(self)
        }
        //头像
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(margin)
            make.top.equalTo(seperateLine.snp_bottom).offset(margin)
            make.width.equalTo(IconWidth)
            make.height.equalTo(IconWidth)
        }
        //认证图标
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView.snp_right)
            make.centerY.equalTo(iconImageView.snp_bottom)
        }
        //昵称
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(margin)
            make.top.equalTo(iconImageView.snp_top)
        }
        //vip图标
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(screenNameLabel.snp_right).offset(margin)
            make.top.equalTo(screenNameLabel.snp_top)
        }
        //创建时间
        createTimeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(margin)
//            make.top.equalTo(screenNameLabel.snp_bottom).offset(margin)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(createTimeLabel.snp_right).offset(margin)
            make.bottom.equalTo(createTimeLabel.snp_bottom)
        }
        
    }
}